package cn.commander.service;

import cn.commander.base.BaseService;
import cn.commander.constant.MvcResponseConstant;
import cn.commander.mapper.UserMapper;
import cn.commander.model.CusPageInfo;
import cn.commander.model.User;
import cn.commander.utils.IdGen;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class UserService extends BaseService<User, String> {

	@Autowired
	private UserMapper userMapper;

	public Object getUserInfo(CusPageInfo cusPageInfo) {
		PageHelper.startPage(cusPageInfo.getCurrentPage(), cusPageInfo.getPageSize());
		List<User> userList = userMapper.findUserInfo();
		cusPageInfo.handleData(userList);
		return getResponseData().buildResponseData(MvcResponseConstant.CODE_00000, null, userList, cusPageInfo);
	}

	public Object getUserByName(String name) {
		return getResponseData().buildResponseData(MvcResponseConstant.CODE_00000, userMapper.getUserByName(name));
	}

	public Object selectByPrimaryKey(String id) {
		if (StringUtils.isEmpty(id)) {
			return getResponseData().buildResponseData(MvcResponseConstant.CODE_89000);
		}
		return getResponseData().buildResponseData(MvcResponseConstant.CODE_00000, userMapper.selectByPrimaryKey(id));
	}
	public Object updateByName(User user) {
		return getResponseData().buildResponseData(MvcResponseConstant.CODE_00000, userMapper.updateByName(user));
	}

	public Object insert(User user) {
		if (user == null || StringUtils.isEmpty(user.getAge()) || StringUtils.isEmpty(user.getName())) {
			return getResponseData().buildResponseData(MvcResponseConstant.CODE_89000, null);
		}
		user.setId(IdGen.uuid());
		user.setCreateBy(user.getId());
		user.setCreateTime(new Date());
		user.setUpdateBy(user.getCreateBy());
		user.setUpdateTime(new Date());
		return getResponseData().buildResponseData(MvcResponseConstant.CODE_00000, userMapper.insert(user));
	}
}
